package com.shop.manager.service.impl;

import com.alibaba.fastjson.JSON;
//import com.shop.manager.entity.*;
//import com.shop.manager.entity.bean.Cart;
import com.base.common.entity.*;
import com.base.common.entity.bean.Cart;
import com.shop.manager.mapper.GoodsDescMapper;
import com.shop.manager.mapper.GoodsMapper;
import com.shop.manager.mapper.ItemCatMapper;
import com.shop.manager.mapper.ItemMapper;
import com.shop.manager.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述：商品service实现类
*/
@Service
public class ItemServiceImpl extends BaseServiceImpl<Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ItemCatMapper itemCatMapper;

    @Autowired
    private GoodsDescMapper goodsDescMapper;

    @Override
    public Map<String, Object> getDynamicPage(Long goodsId) {
        Map<String, Object> map = new HashMap<>();
        // Goods、
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);

        //查询商品的分类  3个分类
        ItemCat itemCat1 = itemCatMapper.selectByPrimaryKey(goods.getCategory1Id());
        ItemCat itemCat2 = itemCatMapper.selectByPrimaryKey(goods.getCategory2Id());
        ItemCat itemCat3 = itemCatMapper.selectByPrimaryKey(goods.getCategory3Id());

        // GoodsDesc、
        GoodsDesc goodsDesc = goodsDescMapper.selectByPrimaryKey(goodsId);

        // List<Item>
        List<Item> itemList = skuList(goodsId);
        map.put("goods", goods);
        map.put("goodsDesc", goodsDesc);
        map.put("itemCat1", itemCat1);
        map.put("itemCat2", itemCat2);
        map.put("itemCat3", itemCat3);
//        map.put("itemList", JSON.toJSONString(itemList));
        map.put("itemList", itemList);

        return map;
    }

    @Override
    public int updateStatus(Long[] ids, String status) {
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(ids));
        //准备修改的数据
        Item item = new Item();
        item.setStatus(status);
        return itemMapper.updateByExampleSelective(item, example);
    }

    /**
     * 判断是否可以扣减库存，执行扣减库存
     *
     * @param cartList
     * @return
     */
    @Override
    public boolean deductionStock(List<Cart> cartList) {
        //检查是否可以扣减库存
        boolean idDeduction = true;//是否可以扣减

        for (Cart cart : cartList) {

            List<OrderItem> orderItemList = cart.getOrderItems();


            for (OrderItem orderItem : orderItemList) {

                //查询库存
                Item sku = itemMapper.selectByPrimaryKey(orderItem.getItemId());
                if (sku == null) {
                    idDeduction = false;
                    break;
                }
                if (!"1".equals(sku.getStatus())) {
                    idDeduction = false;
                    break;
                }
                if (sku.getStockCount().intValue() < orderItem.getNum().intValue()) {
                    idDeduction = false;
                    break;
                }

                //执行扣减
                if (idDeduction) {

                   int newStock = sku.getStockCount() - orderItem.getNum();
                    itemMapper.deductionStock(sku.getId(), newStock); //扣减库存
//                    for (OrderItem orderItem1 : orderItemList) {
//                        itemMapper.deductionStock(orderItem1.getItemId(), orderItem.getNum());//扣减库存
//                        // itemMapper.addSaleNum(orderItem1.getItemId(), orderItem.getNum());//增加销量
//                    }
                }


            }
        }
            return  idDeduction;
    }

    /****
     * 查询对应的SKU列表
     * @param goodsId
     * @return
     */
    private List<Item> skuList(Long goodsId) {
        //select * from tb_item where goods_id=? and status=1 order by is_default desc
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("goodsId", goodsId);
        criteria.andEqualTo("status", "1");
        //设置排序
        example.orderBy("isDefault").desc();
        return itemMapper.selectByExample(example);
    }


}